(function (angular) {
    'use strict';

    angular.module('ytong.controllers').controller('orderCtrl', orderCtrl);

    function orderCtrl(authenticationService, Constants, ordersManager, customerManager, resourceManager, $state) {

        this.getLineStatus = function(line){
            switch (line.status){
                case Constants.LINE_IN_ORDER_STATUS.PENDING_DELIVERY: {
                    return "Pending Delivery";
                    break;
                }
                case Constants.LINE_IN_ORDER_STATUS.DELIVERY_SCHEDULED: {
                    return "Delivery Scheduled";
                    break;
                }
                case Constants.LINE_IN_ORDER_STATUS.DELIVERED: {
                    return "Delivered";
                    break;
                }
                case Constants.LINE_IN_ORDER_STATUS.RETURNED: {
                    return "Returned";
                    break;
                }
            };
        };
        
        this.emptyLine = {};
        this.emptyLine.status = '0';
        this.emptyLine.line_status = "Pending Delivery";

        this.order = ordersManager.getEditOrder();
        ordersManager.setEditOrder(undefined);
        this.products = resourceManager.getResource(Constants.RESOURCES.PRODUCTS);

        this.customers = customerManager.getCustomers();

        this.orderStatus = "";

        if(this.order === undefined){
            this.order = {};
            this.order.status = '0';
            this.order.lines_in_order = [];
            this.order.lines_in_order.push(angular.copy(this.emptyLine));

            this.isEditMode = false;
        }else{

            for(var i=0; i < this.order.lines_in_order.length; i++){
                this.order.lines_in_order[i].line_status = this.getLineStatus(this.order.lines_in_order[i]);
            };

            this.order.removed_lines = [];

            this.isEditMode = true;
        }

        switch (this.order.status){
            case Constants.ORDER_STATUS.OPEN: {
                this.orderStatus = "Open";
                break;
            }
            case Constants.ORDER_STATUS.CLOSED: {
                this.orderStatus = "Closed";
                break;
            }
            case Constants.ORDER_STATUS.IN_PROGRESS: {
                this.orderStatus = "In Progress";
                break;
            }
        };

        this.isEdit = function(){
            return this.isEditMode;
        }

        this.isSalesman = function(){
            return authenticationService.validateUserRole(Constants.ROLES.SALESMAN);
        };

        this.addLine = function(){
            this.order.lines_in_order.push(angular.copy(this.emptyLine));
        };

        this.removeLine = function($index){
            var line = this.order.lines_in_order[$index];
            if(line.line_number !== undefined){
                this.order.removed_lines.push(angular.copy(line));
            };
            this.order.lines_in_order.splice($index, 1);
        };

        this.addOrder = function(){
            ordersManager.addOrder(this.order);
            this.gotoOrdersPage();
        };

        this.cancel = function(){
            this.gotoOrdersPage();
        };

        this.gotoOrdersPage = function(){
            if(this.isSalesman()){
                $state.go(Constants.STATES.SALESMAN.ORDERS)
            }else{
                $state.go(Constants.STATES.MANAGER.ORDERS)
            };
        };

        this.getCost = function(){
            var cost = 0;
            for(var i=0; i < this.order.lines_in_order.length; i++){
                var line = this.order.lines_in_order[i];
                if(line.price !== undefined && line.quantity !== undefined){
                    cost += (line.price * line.quantity);
                }
            };

            return cost;
        };
    };

}(angular));